package com.mall.malladminbackend.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.malladminbackend.dto.SpecKeyDto;
import com.mall.malladminbackend.entity.SpecKey;
import com.mall.malladminbackend.mapper.SpecKeyMapper;
import com.mall.malladminbackend.model.PageBean;
import com.mall.malladminbackend.model.R;
import lombok.AllArgsConstructor;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

/**
 * 作者：tk
 * 时间：2022/8/15 12:00
 * 描述：永无Bug!!!
/**
 * 2022/8/16
 * 一杯茶，一根烟，一个bug解一天
 */
@Service
@AllArgsConstructor
public class SpecKeyService {
    private SpecKeyMapper specKeyMapper;
    //分页查询
    public R getSpecAll(Integer pageNum,Integer pageSize){
        Page<SpecKey> specKeys = PageHelper.startPage(pageNum,pageSize);
        specKeyMapper.selectList(null);
        return R.buildSuccess(PageBean.buildPageBean(specKeys));
    }
    //规格列表添加
    public R addSpec(SpecKeyDto specKeyDto) throws Exception {
        SpecKey specKey = new SpecKey();
        PropertyUtils.copyProperties(specKey,specKeyDto);
        specKeyMapper.insertSpec(specKeyDto);
        return R.buildSuccess();
    }
    //删除
    public R deleteSpec(Integer id){
        specKeyMapper.deleteSpecById(id);
        return R.buildSuccess();
    }

    //根据id查询对应的信息
    public R selectSpecsById(Integer id){
        return R.buildSuccess(specKeyMapper.selectById(id));
    }
    //编辑规格
    public R updateSpecs(SpecKeyDto specKeyDto) throws Exception {
        SpecKey specKey = new SpecKey();
        PropertyUtils.copyProperties(specKey,specKeyDto);
        specKeyMapper.updateById(specKey);
        return R.buildSuccess();
    }

    public R  getAllSpecKey(){
           return R.buildSuccess(specKeyMapper.allspecKey());
       }
}
